package net.alan.modules.service.impl;

import net.alan.common.constants.EProjectLogType;
import net.alan.common.constants.ENoticeType;
import net.alan.common.utils.DateUtil;
import net.alan.modules.bean.*;
import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.repository.TaskCommentRepository;
import net.alan.modules.repository.TaskRepository;
import net.alan.modules.service.IProjectService;
import net.alan.modules.service.INoticeService;
import net.alan.modules.service.ITaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Service
public class TaskServiceImpl extends BaseServiceImpl implements ITaskService {
    private static final Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
    @Autowired
    private TaskRepository taskRepository;
    @Autowired
    private TaskCommentRepository taskCommentRepository;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private INoticeService noticeService;

    @Override
    public JpaRepository getJpaRepository(Class<?> cla) {
        if (isSame(cla, Task.class)) {
            return taskRepository;
        } else if (isSame(cla, TaskComment.class)) {
            return taskCommentRepository;
        }
        return null;
    }

    @Override
    public Page<Task> findTask(Integer userId, Integer projectId, PageModel pageModel) {
        return this.taskRepository.findByProjectIdAndToUserIdOrFromUserId(projectId, userId, pageModel.getPageable());
    }

    @Override
    public Page<TaskComment> findTaskCommentByTaskId(Integer taskId, PageModel pageModel) {
        return this.taskCommentRepository.findByTaskId(taskId, pageModel.getPageable());
    }

    @Override
    public Result saveTask(Task task) {
        if (task.getId() == null) {
            noticeService.addNotice("项目有新任务-" + task.getDetails(), ENoticeType.addTask, task.getId(), task.getToUserId());
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "添加任务-" + task.getName());
            this.taskRepository.save(task);
        } else {
            Task oldTask = this.taskRepository.findOne(task.getId());
            if (task.getToUserId() != oldTask.getToUserId()) {
                this.noticeService.addNotice("项目有新任务-" + task.getName(), ENoticeType.addTask, task.getId(), task.getToUserId());
                this.projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "修改任务-" + task.getName() + "-执行者-" + task.getToUserName());
            }
            if (!task.getPriority().equals(oldTask.getPriority())) {
                this.noticeService.addNotice("任务修改优先级-" + task.getName(), ENoticeType.addTask, task.getId(), task.getToUserId());
                this.projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "修改任务-" + task.getName() + "-优先级-" + task.getPriorityTxt());
            }
            if (task.getEndDate().getTime() != oldTask.getEndDate().getTime()) {
                this.noticeService.addNotice("任务修改截至时间-" + task.getName(), ENoticeType.addTask, task.getId(), task.getToUserId());
                this.projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "修改任务-" + task.getName() + "-截至时间-" + DateUtil.date2Str(task.getEndDate(), "yyyy-MM-dd"));
            }
            oldTask.setToUserId(task.getToUserId());
            oldTask.setEndDate(task.getEndDate());
            oldTask.setPriority(task.getPriority());
            this.taskRepository.save(oldTask);
        }
        return null;
    }

    @Override
    public void afterSave(Object obj) {
        if (isSame(obj.getClass(), TaskComment.class)) {
            TaskComment taskComment = (TaskComment) obj;
            Task task = taskRepository.getOne(taskComment.getTaskId());
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "发布评论-" + task.getName());
        }
    }

    @Override
    public void afterDelete(Object obj) {
        if (isSame(obj.getClass(), Task.class)) {
            Task task = (Task) obj;
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskDelete, task.getName());
        } else if (isSame(obj.getClass(), TaskComment.class)) {
            TaskComment taskComment = (TaskComment) obj;
            Task task = taskRepository.getOne(taskComment.getTaskId());
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskDeleteComment, task.getName());
        }
    }

    @Override
    public List<Object[]> statList(Integer projectId) {
        return this.taskRepository.statList(projectId);
    }

    @Override
    public List<Object[]> statTask(Integer projectId) {
        return this.taskRepository.statTask(projectId);
    }

    @Override
    public Result deleteTaskComment(Integer id) {
        TaskComment attach = getById(TaskComment.class, id);
        if (attach.getFromUser().getId() == WebContextUtil.getUserId()) {
            Task task = getById(Task.class, attach.getTaskId());
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "删除任务评论-" + task.getName());
            return ResultUtil.success();
        }
        return ResultUtil.noRole();
    }


    @Override
    public Result endTask(Integer id) {
        Task task = getById(Task.class, id);
        if (task.getToUserId() == WebContextUtil.getUserId()) {
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "结束任务-" + task.getName());
            task.setDone(true);
            this.taskRepository.save(task);
            return ResultUtil.success();
        }
        return ResultUtil.noRole();
    }

    @Override
    public Result deleteTask(Integer id) {
        Task task = getById(Task.class, id);
        if (task.getFromUserId() == WebContextUtil.getUserId()) {
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.taskcommon, "删除任务-" + task.getName());
            this.taskRepository.delete(id);
            return ResultUtil.success();
        }
        return ResultUtil.noRole();
    }


}
